import { Router } from 'express';
import OSSController from '../controllers/OSSController';
import { uploadSingle, uploadArray, uploadImage, uploadDocument, handleUploadError } from '../middleware/upload';
import { authenticate } from '../middleware/auth';

const router: Router = Router();

/**
 * @swagger
 * components:
 *   schemas:
 *     FileInfo:
 *       type: object
 *       properties:
 *         url:
 *           type: string
 *           description: 文件访问URL
 *         key:
 *           type: string
 *           description: 文件在OSS中的key
 *         size:
 *           type: number
 *           description: 文件大小（字节）
 *         contentType:
 *           type: string
 *           description: 文件MIME类型
 *         bucket:
 *           type: string
 *           description: OSS存储桶名称
 *         etag:
 *           type: string
 *           description: 文件ETag
 *     
 *     ApiResponse:
 *       type: object
 *       properties:
 *         success:
 *           type: boolean
 *           description: 操作是否成功
 *         message:
 *           type: string
 *           description: 响应消息
 *         data:
 *           type: object
 *           description: 响应数据
 *
 * /api/v1/oss/upload:
 *   post:
 *     summary: 上传单个文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *                 description: 要上传的文件
 *               folder:
 *                 type: string
 *                 description: 存储文件夹（可选）
 *               filename:
 *                 type: string
 *                 description: 自定义文件名（可选）
 *               acl:
 *                 type: string
 *                 enum: [private, public-read, public-read-write]
 *                 description: 文件访问权限
 *     responses:
 *       200:
 *         description: 文件上传成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload', authenticate, uploadSingle('file'), handleUploadError, OSSController.uploadFile);

/**
 * @swagger
 * /api/v1/oss/upload/batch:
 *   post:
 *     summary: 批量上传文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               files:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *                 description: 要上传的文件列表
 *               folder:
 *                 type: string
 *                 description: 存储文件夹（可选）
 *               acl:
 *                 type: string
 *                 enum: [private, public-read, public-read-write]
 *                 description: 文件访问权限
 *     responses:
 *       200:
 *         description: 文件批量上传成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/FileInfo'
 */
router.post('/upload/batch', authenticate, uploadArray('files', 10), handleUploadError, OSSController.uploadFiles);

/**
 * @swagger
 * /api/v1/oss/upload/image:
 *   post:
 *     summary: 上传图片文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               image:
 *                 type: string
 *                 format: binary
 *                 description: 要上传的图片文件
 *               folder:
 *                 type: string
 *                 description: 存储文件夹（可选）
 *               filename:
 *                 type: string
 *                 description: 自定义文件名（可选）
 *     responses:
 *       200:
 *         description: 图片上传成功
 */
router.post('/upload/image', authenticate, uploadImage('image'), handleUploadError, OSSController.uploadFile);

/**
 * @swagger
 * /api/v1/oss/upload/document:
 *   post:
 *     summary: 上传文档文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               document:
 *                 type: string
 *                 format: binary
 *                 description: 要上传的文档文件
 *               folder:
 *                 type: string
 *                 description: 存储文件夹（可选）
 *               filename:
 *                 type: string
 *                 description: 自定义文件名（可选）
 *     responses:
 *       200:
 *         description: 文档上传成功
 */
router.post('/upload/document', authenticate, uploadDocument('document'), handleUploadError, OSSController.uploadFile);

/**
 * @swagger
 * /api/v1/oss/download/{key}:
 *   get:
 *     summary: 下载文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件在OSS中的key
 *     responses:
 *       200:
 *         description: 文件下载成功
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 */
router.get('/download/:key(*)', authenticate, OSSController.downloadFile);

/**
 * @swagger
 * /api/v1/oss/info/{key}:
 *   get:
 *     summary: 获取文件信息
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件在OSS中的key
 *     responses:
 *       200:
 *         description: 获取文件信息成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: object
 *                       properties:
 *                         key:
 *                           type: string
 *                         size:
 *                           type: number
 *                         contentType:
 *                           type: string
 *                         lastModified:
 *                           type: string
 *                           format: date-time
 *                         etag:
 *                           type: string
 */
router.get('/info/:key(*)', authenticate, OSSController.getFileInfo);

/**
 * @swagger
 * /api/v1/oss/delete/{key}:
 *   delete:
 *     summary: 删除单个文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件在OSS中的key
 *     responses:
 *       200:
 *         description: 文件删除成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.delete('/delete/:key(*)', authenticate, OSSController.deleteFile);

/**
 * @swagger
 * /api/v1/oss/delete/batch:
 *   post:
 *     summary: 批量删除文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               keys:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: 要删除的文件key列表
 *     responses:
 *       200:
 *         description: 批量删除完成
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: object
 *                       properties:
 *                         deleted:
 *                           type: array
 *                           items:
 *                             type: string
 *                         failed:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               key:
 *                                 type: string
 *                               error:
 *                                 type: string
 */
router.post('/delete/batch', authenticate, OSSController.deleteFiles);

/**
 * @swagger
 * /api/v1/oss/list:
 *   get:
 *     summary: 列出文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: prefix
 *         schema:
 *           type: string
 *         description: 文件前缀过滤
 *       - in: query
 *         name: maxKeys
 *         schema:
 *           type: integer
 *           default: 100
 *         description: 最大返回文件数量
 *       - in: query
 *         name: continuationToken
 *         schema:
 *           type: string
 *         description: 分页令牌
 *     responses:
 *       200:
 *         description: 获取文件列表成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: object
 *                       properties:
 *                         files:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               key:
 *                                 type: string
 *                               size:
 *                                 type: number
 *                               lastModified:
 *                                 type: string
 *                                 format: date-time
 *                               etag:
 *                                 type: string
 *                         nextContinuationToken:
 *                           type: string
 *                         isTruncated:
 *                           type: boolean
 */
router.get('/list', authenticate, OSSController.listFiles);

/**
 * @swagger
 * /api/v1/oss/presigned/{key}:
 *   get:
 *     summary: 生成预签名URL
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件在OSS中的key
 *       - in: query
 *         name: expires
 *         schema:
 *           type: integer
 *           default: 3600
 *         description: URL过期时间（秒）
 *       - in: query
 *         name: method
 *         schema:
 *           type: string
 *           enum: [GET, PUT, POST, DELETE]
 *           default: GET
 *         description: HTTP方法
 *       - in: query
 *         name: contentType
 *         schema:
 *           type: string
 *         description: 内容类型
 *       - in: query
 *         name: process
 *         schema:
 *           type: string
 *         description: 图片处理参数
 *     responses:
 *       200:
 *         description: 生成预签名URL成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: object
 *                       properties:
 *                         url:
 *                           type: string
 *                         expires:
 *                           type: number
 */
router.get('/presigned/:key(*)', authenticate, OSSController.generatePresignedUrl);

/**
 * @swagger
 * /api/v1/oss/copy:
 *   post:
 *     summary: 复制文件
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               sourceKey:
 *                 type: string
 *                 description: 源文件key
 *               targetKey:
 *                 type: string
 *                 description: 目标文件key
 *     responses:
 *       200:
 *         description: 文件复制成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.post('/copy', authenticate, OSSController.copyFile);

/**
 * @swagger
 * /api/v1/oss/exists/{key}:
 *   get:
 *     summary: 检查文件是否存在
 *     tags: [OSS]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件在OSS中的key
 *     responses:
 *       200:
 *         description: 检查文件是否存在成功
 *         content:
 *           application/json:
 *             schema:
 *               allOf:
 *                 - $ref: '#/components/schemas/ApiResponse'
 *                 - type: object
 *                   properties:
 *                     data:
 *                       type: object
 *                       properties:
 *                         exists:
 *                           type: boolean
 */
router.get('/exists/:key(*)', authenticate, OSSController.fileExists);

export default router;